package com.bawei.module_mine.ui.login.view

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.activityViewModels
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.bawei.lib_common.appContext
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.getAttrValue
import com.bawei.lib_common.utils.ValidUtil
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.FragmentPhoneLoginBinding
import com.bawei.module_mine.helper.PopupAgreementHelper
import com.bawei.module_mine.ui.login.LoginViewModel
import com.bawei.myclub.base.mvvm.view.BaseFragment

class PhoneLoginFragment : BaseFragment<LoginViewModel, FragmentPhoneLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_phone_login
    override val mViewModel: LoginViewModel by activityViewModels()
    private lateinit var mNavController: NavController
    private var mLoginAgreement: Boolean = false
    private val mPopupAgreement by lazy {
        PopupAgreementHelper(layoutInflater)
    }

    override fun initView() {
        super.initView()
        mViewModel.setShowingFragmentTag(PhoneLoginFragment::class.simpleName!!)
        mNavController = Navigation.findNavController(mBinding.root)
        initAgreementCheckBox(mLoginAgreement)
        initAgreementView()
    }


    override fun initData() {
        mBinding.viewModel = mViewModel
    }

    override fun initListener() {
        mBinding.apply {
            viewModel = mViewModel
            //隐私政策
            vChecked.setOnClickListener {
                mLoginAgreement = !mLoginAgreement
                initAgreementCheckBox(mLoginAgreement)
            }
            //验证码
            btnSmsCode.setOnClickListener {
                checkPhone()
            }

            etPhone.setOnEditorActionListener { textView, i, keyEvent ->
                if (i == EditorInfo.IME_ACTION_DONE) {
                    checkPhone()
                }
                true
            }
        }
    }

    private fun checkPhone() {
        val phone = mBinding.etPhone.text.toString()
        if (!mLoginAgreement) {
            (activity as BaseActivity<*, *>?)?.dismissKeyboard()
            mPopupAgreement.show(mBinding.btnCheck)
            return
        }
        if (!ValidUtil.phoneNumberValid(phone)) {
            Toast.makeText(context, "手机号码长度错误", Toast.LENGTH_SHORT).show()
            return
        }
        //跳转输入验证码页面
        mViewModel.requestRest = false
        mNavController.navigate(
            R.id.action_phoneLoginFragment_to_smsCodeFragment, bundleOf(
                "action_sms_code" to SmsCode.LOGIN.type
            )
        )
    }

    private fun initAgreementView() {
        val colorMain = getAttrValue(requireContext(), com.bawei.lib_common.R.attr.colorTheme)
        val userAgreement = "《用户协议》"
        val privacyPolicy = "《隐私政策》"
        val content = "阅读并同意$userAgreement 和$privacyPolicy"
        val spannableStringBuilder = SpannableStringBuilder(content)
        val userAgreementStart = content.indexOf(userAgreement)
        val userAgreementEnd = userAgreementStart + userAgreement.length

        val privacyPolicyStart = content.indexOf(privacyPolicy)
        val privacyPolicyEnd = privacyPolicyStart + privacyPolicy.length

        spannableStringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(p0: View) {
                //CommonWebActivity.jumpUserAgreement()
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color = ContextCompat.getColor(requireContext(), colorMain)
                ds.isUnderlineText = false
            }

        }, userAgreementStart, userAgreementEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        spannableStringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(p0: View) {
                //CommonWebActivity.jumpPrivacyAgreement()
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color = ContextCompat.getColor(requireContext(), colorMain)
                ds.isUnderlineText = false
            }

        }, privacyPolicyStart, privacyPolicyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        mBinding.tvAgreement.highlightColor = Color.TRANSPARENT
        mBinding.tvAgreement.text = spannableStringBuilder
        mBinding.tvAgreement.movementMethod = LinkMovementMethod.getInstance()
    }

    private fun initAgreementCheckBox(checked: Boolean) {
        if (!checked) {
            mBinding.btnCheck.background =
                ContextCompat.getDrawable(appContext, R.mipmap.icon_uncheck)
        } else {
            mBinding.btnCheck.background =
                ContextCompat.getDrawable(appContext, R.mipmap.icon_checked)
        }
    }


}